package com.hzh.cusviewdemo.Util;

import android.util.Base64;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;

import javax.crypto.Cipher;

public class RSAUtil {

    public static final String APP_SET_VERSION = "14";  // 配置VER
    public static final String P_R_KY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKHjfkUFEz7ghd+NshiuGV7JAKiZxrukiZnICwpd6oe8WUZD+kLhno6kz7BwEMCQWuEFZZJTlsB4wAjw+TMxSd94DadoGUUPefVFrnrvTxqbsApw4uUlh3GMXcYGPAEzl64kQDiC19Febq1dOWEfIIoHhVUPoi2ARLu7ufqgVbztAgMBAAECgYBoCrPRGdTnmRaQuRkXoXRupVHGCaZeW/2PFrB5xif/ej4J2ZBSQXxMgoF0DGHH0D6H1ZcUtqHxKumIFY6ipu+pJqaAoCKS2AoCwluwGyO6QHIvjMdJAHzq30YarXF5q0nbs4mdXPDRwnHnNf3j0irG+ZgP4naY3ifOGFDBLgDt5QJBAOSvEIbAKyjF6MJWvI1SKHbicxQTDM+6ecUSvgBsBuHxPBao1KboDdoydvAQVZ1jPO0gLbwqOEP4oWFK7g2UCqcCQQC1OeULt3mETqNtj/+9oJmlk0wJs7xOAlY2I1YK+lEML/kOO9TYmUBFtnMfQ4vWc4NgppSfs8ULYJcleIo7TjJLAkEA1MaWx+oxviQ25+uwUIWrJMKadIktGHLXLii8JMReuwer8yFvi0TK7sSZqdBo+KIHZFLWeweFhqjl7xK64GNobQJAMvJrqbeSuddBqnNo+e5EQ3/9SFz2+xc9FUspzTxBvKNf6Nh5JcsvfmmkQ43BqjAfJvSto5fuNRyaYTZZ5gI9sQJAR5qNlTY57Qt0k3uczz7Hhjz7liSfYBBy1/+9YyxlIDNujvf2YACsFCUQqfb6O39co5xZ+j3l1RP46kH7XCB8WA==";

    //将Base64编码后的私钥转换成PrivateKey对象
    public static PrivateKey string2PrivateKey(String priStr) throws Exception {
        byte[] keyBytes = Base64.decode(priStr, Base64.NO_WRAP);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
        return privateKey;
    }

    //私钥解密  主线程会卡顿
    public static String decrypt(String content) {
        try {
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(Cipher.DECRYPT_MODE, string2PrivateKey(P_R_KY));
            byte[] bytes = cipher.doFinal(Base64.decode(content, Base64.NO_WRAP));
            return new String(bytes);
        } catch (Exception e) {
            return "";
        }
    }

}

